const express=require("express")
const router=express.Router();
const pool=require("../pool");


//功能八：用户登录
router.post("/",(req,res)=>{
   //参数
   var uname=req.body.uname;
   var upwd=req.body.upwd;
   //sql
   var sql='SELECT id FROM yhc_user WHERE uname= ? AND upwd=md5(?)';
   pool.query(sql,[uname,upwd],(err,result)=>{
     if(err) throw err;
     if(result==0){
      res.send({code:-1,msg:"用户名或密码有误"})
     }else{
      //将用户登录凭证保存到session中
      var id=result[0].id; //获取当前用户id
      req.session.uid=id;  //保存session
      console.log(req.session.uid);
      res.send({code:id,msg:"登录成功"});
     }
   })
})
module.exports=router;